home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.util.Vector;
-
- class ContextLSystem {
- String axiom;
- Vector rules = new Vector();
- int level;
- StringBuffer currentPath;
-
- public ContextLSystem(Applet app) {
- this.axiom = app.getParameter("axiom");
- int var2 = 1;
-
- while(true) {
- String pred = app.getParameter("pred" + var2);
- String succ = app.getParameter("succ" + var2);
- if (pred == null || succ == null) {
- this.currentPath = new StringBuffer(this.axiom);
- this.level = 0;
- return;
- }
-
- this.rules.addElement(new CLSRule(pred, succ, app.getParameter("lContext" + var2), app.getParameter("rContext" + var2)));
- ++var2;
- }
- }
-
- public int getLevel() {
- return this.level;
- }
-
- public synchronized String getPath() {
- return this.currentPath == null ? null : this.currentPath.toString();
- }
-
- private synchronized void setPath(StringBuffer path) {
- this.currentPath = path;
- ++this.level;
- }
-
- public void generate() {
- StringBuffer newPath = new StringBuffer();
- int pos = 0;
-
- while(pos < this.currentPath.length()) {
- CLSRule rule = this.findRule(pos);
- if (rule == null) {
- newPath.append(this.currentPath.charAt(pos));
- ++pos;
- } else {
- newPath.append(rule.succ);
- pos += rule.pred.length();
- }
- }
-
- this.setPath(newPath);
- }
-
- public CLSRule findRule(int pos) {
- for(int i = 0; i < this.rules.size(); ++i) {
- CLSRule rule = (CLSRule)this.rules.elementAt(i);
- if (rule.matches(this.currentPath, pos)) {
- return rule;
- }
- }
-
- return null;
- }
- }
-